@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo ========================================
echo    多仓库推送工具 (GitHub + Gitee)
echo ========================================
echo.

REM 检查是否有未提交的更改
git status --short >nul 2>&1
if errorlevel 1 (
    echo [错误] 当前目录不是 Git 仓库！
    pause
    exit /b 1
)

REM 显示当前状态
echo.
echo [信息] 当前仓库状态：
echo.
git status --short
echo.

REM 询问是否继续
set /p continue="是否继续提交并推送？(y/n): "
if /i not "%continue%"=="y" (
    echo [取消] 操作已取消
    pause
    exit /b 0
)

echo.
echo ----------------------------------------

REM 输入 commit message
set /p commit_msg="请输入 commit 信息: "
if "%commit_msg%"=="" (
    echo [错误] commit 信息不能为空！
    pause
    exit /b 1
)

echo.
echo ----------------------------------------

REM 选择推送目标
echo 请选择推送目标：
echo   1. 仅推送到 GitHub
echo   2. 仅推送到 Gitee
echo   3. 同时推送到 GitHub 和 Gitee (默认)
echo.
set /p push_target="请选择 [1/2/3]: "
if "%push_target%"=="" set push_target=3

echo.
echo ========================================
echo [步骤 1/3] 添加所有更改到暂存区...
echo ========================================
git add .
if errorlevel 1 (
    echo [错误] git add 失败！
    pause
    exit /b 1
)
echo [成功] 已添加所有更改
echo.

echo ========================================
echo [步骤 2/3] 提交更改...
echo ========================================
git commit -m "%commit_msg%"
if errorlevel 1 (
    echo [警告] 没有需要提交的更改，或提交失败
    echo.
    set /p force_push="是否强制推送现有提交？(y/n): "
    if /i not "!force_push!"=="y" (
        echo [取消] 操作已取消
        pause
        exit /b 0
    )
) else (
    echo [成功] 提交成功
)
echo.

echo ========================================
echo [步骤 3/3] 推送到远程仓库...
echo ========================================

REM 根据选择推送
if "%push_target%"=="1" (
    echo [推送] 正在推送到 GitHub...
    git push github main
    if errorlevel 1 (
        echo [错误] 推送到 GitHub 失败！
        pause
        exit /b 1
    )
    echo [成功] 已推送到 GitHub
) else if "%push_target%"=="2" (
    echo [推送] 正在推送到 Gitee...
    git push gitee main
    if errorlevel 1 (
        echo [错误] 推送到 Gitee 失败！
        pause
        exit /b 1
    )
    echo [成功] 已推送到 Gitee
) else (
    echo [推送] 正在推送到 GitHub...
    git push github main
    if errorlevel 1 (
        echo [错误] 推送到 GitHub 失败！
        set github_failed=1
    ) else (
        echo [成功] 已推送到 GitHub
    )
    echo.
    
    echo [推送] 正在推送到 Gitee...
    git push gitee main
    if errorlevel 1 (
        echo [错误] 推送到 Gitee 失败！
        set gitee_failed=1
    ) else (
        echo [成功] 已推送到 Gitee
    )
    
    if defined github_failed if defined gitee_failed (
        echo.
        echo [错误] 两个仓库推送都失败！
        pause
        exit /b 1
    )
    
    if defined github_failed (
        echo.
        echo [警告] GitHub 推送失败，但 Gitee 推送成功
    )
    
    if defined gitee_failed (
        echo.
        echo [警告] Gitee 推送失败，但 GitHub 推送成功
    )
)

echo.
echo ========================================
echo           推送完成！
echo ========================================
echo.
pause

